import 'package:flutter/material.dart';

import 'package:flutter_ume_kit_mock/data/mock_config_manager.dart';
import 'package:flutter_ume_kit_mock/data/model/case.dart';
import 'package:flutter_ume_kit_mock/data/model/mock.dart';
import 'package:flutter_ume_kit_mock/widgets/mock_state.dart';

class CaseItem extends StatelessWidget {
  final MockCase mockCase;
  final MockClickListener mockClickListener;

  const CaseItem(this.mockCase, this.mockClickListener, {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ColoredBox(
      color: mockCase.enable ? Colors.yellow.shade300 : Colors.blueGrey.shade50,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              //标题
              Expanded(child: _caseTitleText()),
              //开关
              _switchButton()
            ],
          ),
          //描述
          if (mockCase.fields?.desc != null) _caseDescText(),
          //包含的mocks
          Padding(
            padding: const EdgeInsets.only(top: 10, bottom: 15),
            child: _containMocks(),
          ),
          SizedBox(
            height: 1,
            width: double.infinity,
            child: ColoredBox(
              color: Colors.grey.shade300,
            ),
          )
        ],
      ),
    );
  }

  ///标题
  Widget _caseTitleText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16),
      child: Text(
        "${mockCase.fields?.title}",
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(
            color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
      ),
    );
  }

  ///描述
  Widget _caseDescText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16),
      child: Text(
        mockCase.fields!.desc!,
        style: TextStyle(
            color: Colors.black.withOpacity(0.6),
            fontSize: 13,
            fontWeight: FontWeight.w600),
      ),
    );
  }

  ///展示包含的mock列表
  Widget _containMocks() {
    if (mockCase.mocks == null) {
      return const SizedBox.shrink();
    }
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Padding(
        padding: const EdgeInsets.only(left: 10, right: 10),
        child: Material(
          type: MaterialType.transparency,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: mockCase.mocks!
                .map((e) => _mockItem(e))
                .toList(growable: false),
          ),
        ),
      ),
    );
  }

  Widget _mockItem(Mock mock) {
    return Container(
      height: 35,
      margin: const EdgeInsets.only(left: 6, right: 6),
      child: Ink(
        decoration: BoxDecoration(
            color: Colors.blue.shade100,
            borderRadius: const BorderRadius.all(Radius.circular(100))),
        child: InkResponse(
          highlightShape: BoxShape.rectangle,
          borderRadius: const BorderRadius.all(Radius.circular(100)),
          child: Padding(
            padding: const EdgeInsets.only(left: 5, right: 10),
            child: Row(mainAxisSize: MainAxisSize.min, children: [
              //模块
              Container(
                  height: 25,
                  padding: const EdgeInsets.only(left: 6, right: 6),
                  margin: const EdgeInsets.only(right: 5),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Colors.blue.shade300,
                      borderRadius: const BorderRadius.all(Radius.circular(100))),
                  child: Text(
                    mock.fields?.module ?? "",
                    style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 13,
                        color: Colors.white),
                    strutStyle:
                        const StrutStyle(leading: 0, forceStrutHeight: true),
                  )),
              //title
              Text(
                mock.fields?.title ?? "",
                style: const TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.bold,
                    color: Colors.deepPurpleAccent),
              ),
            ]),
          ),
          onTap: () {
            mockClickListener(mock);
          },
        ),
      ),
    );
  }

  ///开关
  Widget _switchButton() {
    return Switch(
        value: mockCase.enable,
        onChanged: (value) {
          if (value &&
              MockConfigManager.checkEnableConflict(mockCase: mockCase)) {
            //冲突
            return;
          }
          mockCase.enable = value;
          if (value) {
            MockConfigManager.addEnable(mockCase.id);
          } else {
            MockConfigManager.removeEnable(mockCase.id);
          }
          MockConfigManager.caseListNotifier.notifyListeners();
        });
  }
}
